package com.distribution.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.distribution.common.exception.ServiceException;
import com.distribution.system.domain.entity.CmccDaAreaFlow;
import com.distribution.system.domain.entity.CmccDaProduct;
import com.distribution.system.mapper.CmccDaAreaFlowMapper;
import com.distribution.system.service.AddressSystemService;
import com.distribution.system.service.CmccDaAreaFlowService;
import com.distribution.system.service.CmccDaProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Objects;


@Service("cmccDaAreaFlowService")
public class CmccDaAreaFlowServiceImpl extends ServiceImpl<CmccDaAreaFlowMapper, CmccDaAreaFlow> implements CmccDaAreaFlowService {

    @Autowired
    private CmccDaProductService cmccDaProductService;

    @Autowired
    private AddressSystemService addressSystemService;

    @Override
    public void updateAreaFlow(Integer id, Integer areaFlowId, String provinceCode, String cityCode) {
        //首先当前号卡和分流号卡必须存在
        CmccDaProduct currentCard = cmccDaProductService.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId,id),false);
        CmccDaProduct areaFlowCard = cmccDaProductService.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId,areaFlowId),false);
        //如果能查到则更新
        CmccDaAreaFlow cmccDaAreaFlow = getOne(new LambdaQueryWrapper<CmccDaAreaFlow>().eq(CmccDaAreaFlow::getProductId, id)
                .eq(CmccDaAreaFlow::getAreaFlowProductId, areaFlowId),false);
        if (cmccDaAreaFlow == null){
            throw new ServiceException("当前分流信息不存在");
        }
        cmccDaAreaFlow.setProductId(id);
        cmccDaAreaFlow.setProductName(currentCard.getProductName());
        cmccDaAreaFlow.setAreaFlowProductId(areaFlowId);
        cmccDaAreaFlow.setAreaFlowProductName(areaFlowCard.getProductName());
        cmccDaAreaFlow.setAreaFlowProvinceCode(provinceCode);
        cmccDaAreaFlow.setAreaFlowCityCode(cityCode);
        //设置省市名称
        cmccDaAreaFlow.setAreaFlowProvinceName(addressSystemService.getProvinceNameByCode(provinceCode));
        cmccDaAreaFlow.setAreaFlowCityName(addressSystemService.getCityNameByCode(cityCode));
        cmccDaAreaFlow.setCreateTime(LocalDateTime.now());
        cmccDaAreaFlow.setUpdateTime(LocalDateTime.now());
        updateById(cmccDaAreaFlow);
    }

    @Override
    public void addAreaFlowInfo(Integer id, Integer areaFlowId, String provinceCode, String cityCode) {
        //System.out.println(id);
        //System.out.println(areaFlowId);
        //首先当前号卡和分流号卡必须存在
        CmccDaProduct currentCard = cmccDaProductService.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId,id),false);
        CmccDaProduct areaFlowCard = cmccDaProductService.getOne(new LambdaQueryWrapper<CmccDaProduct>().eq(CmccDaProduct::getId,areaFlowId),false);
        if (currentCard == null || areaFlowCard == null)
            throw new ServiceException("当前卡或分流卡不存在");

        //其次确保当前分流信息不存在
        CmccDaAreaFlow areaFlow = getOne(new LambdaQueryWrapper<CmccDaAreaFlow>()
                .eq(CmccDaAreaFlow::getProductId, id)
                .eq(CmccDaAreaFlow::getAreaFlowProductId, areaFlowId)
                .eq(!Objects.isNull(provinceCode), CmccDaAreaFlow::getAreaFlowProvinceCode,provinceCode)
                .eq(!Objects.isNull(cityCode), CmccDaAreaFlow::getAreaFlowCityCode,cityCode), false);
        if (areaFlow != null) throw new ServiceException("当前分流信息已存在");

        //设置当前卡的分流开关打开
        currentCard.setAreaFlowFlag("1");
        cmccDaProductService.updateById(currentCard);

        //再者当前号卡与分流号卡的基础佣金要一致
        /*if (!Objects.equals(currentCard.getBasicsCommission(), areaFlowCard.getBasicsCommission())){
            throw new ServiceException("当前卡与分流卡基础佣金不一致");
        }*/

        //再者不能分流到自己
        if (Objects.equals(id, areaFlowId)) throw new ServiceException("无法分流到号卡本身");

        //添加绑定号卡记录
        CmccDaAreaFlow cmccDaAreaFlow = new CmccDaAreaFlow();
        cmccDaAreaFlow.setProductId(id);
        cmccDaAreaFlow.setProductName(currentCard.getProductName());
        cmccDaAreaFlow.setAreaFlowProductId(areaFlowId);
        cmccDaAreaFlow.setAreaFlowProductName(areaFlowCard.getProductName());
        cmccDaAreaFlow.setAreaFlowProvinceCode(provinceCode);
        cmccDaAreaFlow.setAreaFlowCityCode(cityCode);
        //设置省市名称
        cmccDaAreaFlow.setAreaFlowProvinceName(addressSystemService.getProvinceNameByCode(cmccDaAreaFlow.getAreaFlowProvinceCode()));
        cmccDaAreaFlow.setAreaFlowCityName(addressSystemService.getCityNameByCode(cmccDaAreaFlow.getAreaFlowCityCode()));
        cmccDaAreaFlow.setCreateTime(LocalDateTime.now());
        cmccDaAreaFlow.setUpdateTime(LocalDateTime.now());
        save(cmccDaAreaFlow);
    }

    @Override
    public List<CmccDaAreaFlow> getAreaFlowInfoListByProductId(Integer productId) {
        //根据当前卡id查询分流信息
        List<CmccDaAreaFlow> cmccDaAreaFlowList = list(new LambdaQueryWrapper<CmccDaAreaFlow>()
                .eq(CmccDaAreaFlow::getProductId, productId)
                .orderByDesc(CmccDaAreaFlow::getCreateTime));
        if (cmccDaAreaFlowList == null || cmccDaAreaFlowList.isEmpty()) throw new ServiceException("当前产品没有地区分流信息");
        return cmccDaAreaFlowList;
    }
}
